#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#if !wxUSE_GLCANVAS
#error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
#endif

////@begin includes
#include "OryxApp.h"

#include "wxlua/wxlua.h"

#include "wxbind/include/wxbinddefs.h"
WXLUA_DECLARE_BIND_ALL
////@end includes


wxColour colorCaption;
wxColour colorCaptionText;
wxColour colorBgContent;
wxColour subCategoryColor;




/*!
 * Application instance implementation
 */

////@begin implement app
IMPLEMENT_APP( OryxApp )
////@end implement app

/*!
 * OryxApp type definition
 */

IMPLEMENT_CLASS( OryxApp, wxApp )

/*!
 * OryxApp event table definition
 */

BEGIN_EVENT_TABLE( OryxApp, wxApp )

////@begin OryxApp event table entries
////@end OryxApp event table entries

	/*EVT_END_SESSION(OryxApp::OnEndSession)
    EVT_QUERY_END_SESSION(OryxApp::OnQueryEndSession)*/

END_EVENT_TABLE()

/*!
 * Constructor for OryxApp
 */

OryxApp::OryxApp()
{
////@begin OryxApp member initialisation
////@end OryxApp member initialisation
}

/*!
 * Initialisation for OryxApp
 */

bool OryxApp::OnInit()
{
// 	if ( !wxApp::OnInit() )
// 		return false;

	InitColors();

	//Get application directory
	wxFileName arg(argv[0]);
	applicationDir = arg.GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME );


	//Load resources
	wxXmlResource::Get()->InitAllHandlers(); 
	bool ret = wxXmlResource::Get()->Load("resources.xrc"); 

	//Create the frame
	mainFrame = new MainFrame( applicationDir, ID_FRAME );
	SetTopWindow(mainFrame);
	mainFrame->Show(true);
	
	return InitLuaState();

	return true;
}

bool OryxApp::InitLuaState()
{
	WXLUA_IMPLEMENT_BIND_ALL

	wxLuaState::sm_wxAppMainLoop_will_run = true;

	state = new wxLuaState(this, wxID_ANY);
	if (!state->Ok())
		return false;

	lua_State* L = state->GetLuaState();

	int type = wxluaT_gettype(L, "wxFrame");
	bool b = state->wxluaT_PushUserDataType(mainFrame, type, true);
	int top = state->lua_GetTop();
	lua_setglobal(L,"mainFrame");

	//int ret = state->RunFile(wxT(".\\scripts\\test.lua"));

	int ret = luaL_dofile(L, "scripts\\test.lua");
	if (ret)
	{
		int top = state->lua_GetTop();
		const char* err = lua_tostring(L, -1);
		wxString msg = wxString(err);
		wxMessageBox(msg);
		return false;
	}

	return true;
}

void OryxApp::InitColors()
{
	// Remove the comment markers above and below this block
    // to make permanent changes to the code.


#if wxUSE_XPM
    wxImage::AddHandler( new wxXPMHandler );
#endif
#if wxUSE_LIBPNG
    wxImage::AddHandler( new wxPNGHandler );
#endif
#if wxUSE_LIBJPEG
    wxImage::AddHandler( new wxJPEGHandler );
#endif
#if wxUSE_GIF
    wxImage::AddHandler( new wxGIFHandler );
#endif

	//Define colors
	wxIFMCaptionConfig &caption_config = wxIFMDefaultPanelPlugin::GetCaptionConfig();
	

	//Light blue
	/*wxBorder_Pen		= wxColour(144, 152, 160);
	wxBorder_PenDark	= wxColour(144, 152, 160);
	wxBackground_Pen	= wxColour(144, 152, 160);

	wxResizer_Pen		= wxColour(143, 143, 143);
	wxResizerBorder_Pen	= wxColour(100, 100, 100);

	colorCaption		= wxColour(144, 152, 160);
	colorCaptionText	= wxColour(0, 0, 0);

	wxTab_border_p		= wxColour(100, 100, 100);
	wxTab_active_bg_b	= wxColour(210, 210, 210);
	wxTab_active_bg_p	= wxColour(144, 152, 160);

	wxTab_inactive_bg_b	= wxColour(140, 140, 140);
	wxTab_inactive_bg_p	= wxTab_inactive_bg_b;

	wxTab_bg_brush		= wxColour(140, 140, 140);
	wxTab_bg_pen		= wxColour(144, 152, 160);

	colorBgContent		= wxColour(144, 152, 160);/**/

	//Gray border
	/*wxBorder_Pen		= wxColour(100, 100, 100);
	wxBorder_PenDark	= wxColour(100, 100, 100);
	wxBackground_Pen	= wxColour(167, 167, 167);

	wxResizer_Pen		= wxColour(150, 150, 150);
	wxResizerBorder_Pen	= wxColour(100, 100, 100);

	colorCaption		= wxColour(167, 167, 167);
	colorCaptionText	= wxColour(0, 0, 0);

	wxTab_border_p		= wxColour(100, 100, 100);
	wxTab_active_bg_b	= wxColour(210, 210, 210);
	wxTab_active_bg_p	= wxColour(167, 167, 167);

	wxTab_inactive_bg_b	= wxColour(140, 140, 140);
	wxTab_inactive_bg_p	= wxTab_inactive_bg_b;

	wxTab_bg_brush		= wxColour(140, 140, 140);
	wxTab_bg_pen		= wxColour(167, 167, 167);

	colorBgContent		= wxColour(167, 167, 167);/**/

	

	//Flat
	/*wxBorder_Pen		= wxColour(167, 167, 167);
	wxBorder_PenDark	= wxColour(167, 167, 167);
	wxBackground_Pen	= wxColour(167, 167, 167);

	wxResizer_Pen		= wxColour(167, 167, 167);
	wxResizerBorder_Pen	= wxColour(100, 100, 100);

	colorCaption		= wxColour(167, 167, 167);
	colorCaptionText	= wxColour(0, 0, 0);

	wxTab_border_p		= wxColour(100, 100, 100);
	wxTab_active_bg_b	= wxColour(210, 210, 210);
	wxTab_active_bg_p	= wxColour(167, 167, 167);

	wxTab_inactive_bg_b	= wxColour(140, 140, 140);
	wxTab_inactive_bg_p	= wxTab_inactive_bg_b;

	wxTab_bg_brush		= wxColour(140, 140, 140);
	wxTab_bg_pen		= wxColour(167, 167, 167);

	colorBgContent		= wxColour(167, 167, 167);/**/



	//Black Orange
	wxBorder_Pen		= wxColour(104, 104, 104);
	wxBorder_PenDark	= wxColour(wxBorder_Pen.Red()/2, wxBorder_Pen.Green()/2, wxBorder_Pen.Blue()/2);
	wxBackground_Pen	= wxColour(72, 72, 72);

	wxResizer_Pen		= wxBackground_Pen;
	wxResizerBorder_Pen	= wxBorder_Pen;

	colorCaption		= wxColour(48, 48, 48);
	colorCaptionText	= wxColour(220, 220, 220);

	wxTab_border_p		= wxColour(72, 72, 72);
	wxTab_active_bg_b	= wxColour(244, 156, 28);
	wxTab_active_bg_p	= wxColour(96, 96, 96);
	wxTab_inactive_bg_b	= wxColour(174, 174, 174);
	wxTab_inactive_bg_p	= wxTab_inactive_bg_b;
	wxTab_bg_brush		= wxColour(96, 96, 96);
	wxTab_bg_pen		= wxColour(96, 96, 96);

	colorBgContent		= wxColour(176, 184, 192);//wxColour(144, 152, 160);
	subCategoryColor		= wxColour(255,255,183);
	caption_config.set_3d_boder(false);
	
	//Gradient
	caption_config.set_caption_style(IFM_CAPTION_GRADIENT);
	caption_config.set_caption_color(colorCaption);
	caption_config.set_caption_gradient_color(wxTab_bg_brush);/**/


	//Gray border thin
	/*wxColour			backGround(167, 167, 167);
	wxBorder_Pen		= wxColour(backGround);
	wxBorder_PenDark	= wxColour(backGround);
	wxBackground_Pen	= wxColour(backGround);

	wxResizer_Pen		= wxColour(backGround);
	wxResizerBorder_Pen	= wxColour(backGround);

	colorCaption		= wxColour(backGround);
	colorCaptionText	= wxColour(0, 0, 0);

	wxTab_border_p		= wxColour(100, 100, 100);
	wxTab_active_bg_b	= wxColour(210, 210, 210);
	wxTab_active_bg_p	= wxColour(backGround);

	wxTab_inactive_bg_b	= wxColour(140, 140, 140);
	wxTab_inactive_bg_p	= wxTab_inactive_bg_b;

	wxTab_bg_brush		= wxColour(140, 140, 140);
	wxTab_bg_pen		= wxColour(backGround);

	colorBgContent		= wxColour(backGround);
	
	caption_config.set_3d_boder(true);
	
	//Solid
	caption_config.set_caption_color(wxTab_bg_brush);

	/**/
}

int OryxApp::OnExit()
{
	state->CloseLuaState(true);
	state->Destroy();
	delete state;

	return wxApp::OnExit();
}